<?php

class lottosb
{
    public $m_lotto_id = 0;
    public $autoport = 0;
    public $betmin = 5;
    public $betmin2face = 5;
    public $on3 = -1;
    public $on2 = -1;
    public $on1 = -1;
   	public $sb = NULL; //单双
	public $bs = NULL; //大小
	public $dz = NULL; //对子
	public $bz = NULL; //豹子
	public $zd = NULL; //总点数
	
    public $oamount = 0;
    public $omb = 0;
    public $oad = 0;
    public $oua = 0;
    public $our = 0;
    public $ogg = 0;
    public $openDate = NULL;
    public $bettotal_by_lotto_id = false;
    public $bettotal_3d = "";
    public $tbl = NULL;
    public $gametype = NULL;
    public $g_2face_sb = NULL;
    public $show5N = false;
    public $betname_3d = array();

    public function lottosb($gametype = 1)
    {
        global $g_gametype_3d;
        global $g_2face_sb;
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->gametype = $gametype;
        $this->g_2face_sb = $g_2face_sb;
        $this->show5N = false;
        $this->betname_3d = judge_type($gametype);
    }

    public function GetConfig($id)
    {
        $rs = getdb("Select value From k_config Where id = '{$id}'");
        if ($rs->eof)
        {
            return 0;
        }
        return $rs->f['value'];
    }

    public function GetAutoRateConfig($bettype, $number = "", $subid = 0)
    {
        $numArr = array();
        $rs = getdb("!!Select * From autorate_config_{$this->tbl} Where bettype_id={$bettype} AND subid={$subid} LIMIT 1");
        if (!$rs->eof)
        {
            return $rs;
        }
        return false;
    }

    public function GetAutoRateConfigSS($bettype, $user_id, $subid = 0)
    {
        $val = 0;
        $rs = getdb("!!Select * From autorate_config_ss Where gametype={$this->gametype} and user_id = {$user_id} and bettype={$bettype} and subid={$subid} LIMIT 1");
        if (!$rs->eof)
        {
            return $rs;
        }
        return false;
    }

    public function GetUserID($user_id)
    {
        $ret = array();
        $sql = "select A.id as id_0, B.id as id_1,C.id as id_2,D.id as id_3,E.id as id_4, 0 as id_5 from k_user A";
        $sql .= " left join k_user B ON B.id = A.prev_id";
        $sql .= " left join k_user C ON C.id = B.prev_id";
        $sql .= " left join k_user D ON D.id = C.prev_id";
        $sql .= " left join k_user E ON E.id = D.prev_id";
        $sql .= " where E.id is not NULL";
        $sql .= " and A.id = " . $user_id;
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $i = 1;
            while ($i <= 5)
            {
                if (empty($rs->f["id_" . $i]))
                {
                    array_push($ret, 0);
                } else
                {
                    array_push($ret, $rs->f["id_" . $i]);
                }
                ++$i;
            }
        }
        return $ret;
    }

    public function GetAgentID($user_id)
    {
        $ret = array();
        $sql = "select A.id as id_0, B.id as id_1,C.id as id_2,D.id as id_3,E.id as id_4, 0 as id_5 from k_user A";
        $sql .= " left join k_user B ON B.id = A.prev_id";
        $sql .= " left join k_user C ON C.id = B.prev_id";
        $sql .= " left join k_user D ON D.id = C.prev_id";
        $sql .= " left join k_user E ON E.id = D.prev_id";
        $sql .= " Where A.id =" . $user_id;
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $i = 1;
            while ($i <= 5)
            {
                if (empty($rs->f["id_" . $i]))
                {
                    array_push($ret, $rs->f["id_" . $i]);
                }
                ++$i;
            }
        }
        return $ret;
    }

    public function GetAgentUserIDScript($user_id, $acl)
    {
        $sl = "select A.id from k_user A";
        $sl .= " left join k_user B ON B.id = A.prev_id";
        $sl .= " left join k_user C ON C.id = B.prev_id";
        $sl .= " left join k_user D ON D.id = C.prev_id";
        $sl .= " left join k_user E ON E.id = D.prev_id";
        switch ($acl)
        {
            case 2 :
                $sl .= " Where B.id IS NOT NULL and B.id = " . $user_id;
                break;
            case 3 :
                $sl .= " Where C.id IS NOT NULL and C.id = " . $user_id;
                break;
            case 4 :
                $sl .= " Where D.id IS NOT NULL and D.id = " . $user_id;
                break;
            case 5 :
                $sl .= " Where E.id IS NOT NULL and E.id = " . $user_id;
                break;
        }
        $rs = getdb($sl);
        $tmpArr = array();
        while (!$rs->eof)
        {
            array_push($tmpArr, $rs->f[0]);
            $rs->next();
        }
        $cmdStr = "(" . implode(",", $tmpArr) . ")";
        return $cmdStr;
    }

    public function GetUserAmounts($user_id, $bettype, $tagStr, $subid = 0)
    {
        $amt = 0;
        $rs = getdb("Select A.* From amounts_{$this->tbl} A Left Join k_user_percent B ON B.user_id = A.user_id Where A.user_id=" . $user_id . " and A.bettype=" . $bettype . " and A.subid=" . $subid . " AND IFNULL(B.percent_sw,0)=0 AND B.gametype = {$this->gametype}");
        if (!$rs->eof)
        {
            $amt = $rs->f[$tagStr];
        }
        return $amt;
    }

    public function GetBackArr($user_id, $bettype, $subid, $disk, $acl)
    {
        static $backArr = NULL;
        $mod = 0;
        if ($acl == 255)
        {
            if ($user_id == "")
            {
                return 0;
            }
            $rs = getdb("select id from k_user where acl=255 and account='{$user_id}' limit 1");
            if (!$rs->eof)
            {
                $user_id = $rs->f['id'];
            }
            $mod = 255;
            $rs->close();
        }
        if (!isset($backArr))
        {
            $backArr = array();
        }
        if (!isset($backArr[$user_id]))
        {
            $backArr[$user_id] = GetAmountSetting($user_id, $this->gametype, $mod);
            if ($backArr[$user_id] == false)
            {
                diemsg("没有设定退水：user id = {$user_id}");
            }
        }
        $betSubIdx = $bettype . "_" . $subid;
        if ($acl == 255 && $backArr[$user_id][$betSubIdx]["back" . $disk] == 100)
        {
            return 0;
        }
        return $backArr[$user_id][$betSubIdx]["back" . $disk];
    }

    public function GetCurrectRate($disk, $user_id, $lotto_id, $bettype, $numbers, $subid = 0)
    {
        $cdisk = "abcd";
        $l = 0;
        $sql = "Select * From rate_{$this->tbl}";
        $sql .= " Where lotto_id={$lotto_id} and bettype = {$bettype} and subid = {$subid} and disk = {$disk}";
        $rs = getdb($sql);
        if ($rs->eof)
        {
            return 0;
        }
        $ratenumArr = array(13, 14, 15, 16, 20, 21, 35);
        $ratenumRateArr = array();
        $ratenumRateSArr = array();
        if (in_array($bettype, $ratenumArr))
        {
            $sql = "Select num, rate From ratenum_{$this->tbl}";
            $sql .= " Where lotto_id={$lotto_id} and bettype = {$bettype} and subid = {$subid} and disk = {$disk} and num in ({$numbers})";
            $rs = getdb($sql);
            while (!$rs->eof)
            {
                $numTemp = intval($rs->f[0]);
                $rateTemp = $rs->f[1];
                $ratenumRateArr[$numTemp] = $rateTemp;
                $rs->next();
            }
            if ($user_id !== 0)
            {
                $sql = "Select num, rate From ratenums_{$this->tbl}";
                $sql .= " Where lotto_id={$lotto_id} and bettype = {$bettype} and subid = {$subid} and user_id = {$user_id} and num in ({$numbers})";
                $rs = getdb($sql);
                while (!$rs->eof)
                {
                    $numTemp = intval($rs->f[0]);
                    $rateTemp = $rs->f[1];
                    $ratenumRateSArr[$numTemp] = $rateTemp;
                    $rs->next();
                }
            }
        }
        $rates = array();
        $i = 0;
        while ($i < 22)
        {
            $itemp = "r" . str_pad($i, 2, "0", STR_PAD_LEFT);
            $rates[$itemp] = $rs->f[$itemp];
            ++$i;
        }
        if ($user_id !== 0)
        {
            $sql = "Select * From rates_{$this->tbl} ";
            $sql .= " Where lotto_id={$lotto_id} and user_id={$user_id} and bettype = " . $bettype . " and subid = {$subid}";
            $rs2 = getdb($sql);
            if (!$rs2->eof)
            {
                $i = 0;
                while ($i < 22)
                {
                    $itemp = "r" . str_pad($i, 2, "0", STR_PAD_LEFT);
                    $rates -= $itemp;
                    ++$i;
                }
            }
        }
        $num = array();
        if ($bettype == 46)
        {
            $num = explode(",", $numbers);
        } else if (3 < strlen($numbers))
        {
            $i = 0;
            while ($i < strlen($numbers))
            {
                array_push($num, substr($numbers, $i, 1));
                ++$i;
            }
        } else
        {
            array_push($num, $numbers);
        }
        $rate = 99999;
        $cNumTemp = count($num);
        $i = 0;
        while ($i < $cNumTemp)
        {
            $ss = $num[$i];
            $ssIdx = "r" . str_pad($ss, 2, "0", STR_PAD_LEFT);
            switch ($bettype)
            {
                case 13 :
                case 14 :
                case 15 :
                case 16 :
                    $ssInt = intval($ss);
                    $rateSub = isset($ratenumRateArr[$ssInt]) ? $ratenumRateArr[$ssInt] : 0;
                    $rateSSub = isset($ratenumRateSArr[$ssInt]) ? $ratenumRateSArr[$ssInt] : 0;
                    if (substr($num[$i], 0, 1) == substr($num[$i], 1, 1))
                    {
                        $rate = $rates['r02'] - $rateSub - $rateSSub;
                    } else
                    {
                        $rate = $rates['r01'] - $rateSub - $rateSSub;
                    }
                    break;
                case 20 :
                case 21 :
                case 35 :
                    $ssInt = intval($ss);
                    $rateSub = isset($ratenumRateArr[$ssInt]) ? $ratenumRateArr[$ssInt] : 0;
                    $rateSSub = isset($ratenumRateSArr[$ssInt]) ? $ratenumRateSArr[$ssInt] : 0;
                    if (substr($num[$i], 0, 1) == substr($num[$i], 1, 1) && substr($num[$i], 0, 1) == substr($num[$i], 2, 1))
                    {
                        $rate = $rates['r03'] - $rateSub - $rateSSub;
                    } else if (substr($num[$i], 0, 1) == substr($num[$i], 1, 1) || substr($num[$i], 0, 1) == substr($num[$i], 2, 1) || substr($num[$i], 1, 1) == substr($num[$i], 2, 1))
                    {
                        $rate = $rates['r02'] - $rateSub - $rateSSub;
                    } else
                    {
                        $rate = $rates['r01'] - $rateSub - $rateSSub;
                    }
                    break;
                case 46 :
                    $rateTemp = $rates[$ssIdx];
                    $rate = $rate == 99999 ? $rateTemp : $rateTemp * $rate;
                    break;
                default :
                    $rate = min($rate, $rates[$ssIdx]);
                    break;
            }
            ++$i;
        }
        if ($rate == 99999)
        {
            $rate = 0;
        }
        return $rate;
    }

    public function GetBetTotalFromAgentId($acl, $u_id, $rs)
    {
        $amt = 0;
        $fn = "";
        $s = $this->GetAgentUserIDScript($u_id, $acl);
        switch ($acl)
        {
            case 2 :
                $fn = "percent2";
                break;
            case 3 :
                $fn = "percent3";
                break;
            case 4 :
                $fn = "percent4";
                break;
            case 5 :
                $fn = "percent5";
                break;
            case 6 :
            case 255 :
                $fn = "percent255";
                break;
        }
        $sql = "Select SUM(amount*" . $fn . "/100" . ") as amount From bet_{$this->tbl} Where lotto_id=" . $this->m_lotto_id . " and bettype=" . $rs->f['bettype'] . " and subid=" . $rs->f['subid'] . " and numbers='" . $rs->f['numbers'] . "' and user_id in " . $s;
        $q = getdb($sql);
        $amt = isset($q->f['amount']) ? $q->f['amount'] : "0";
        if (3 <= $acl)
        {
            $sql = "Select SUM(amount*" . $fn . "/100" . ") as amount From bet2_{$this->tbl} Where lotto_id=" . $this->m_lotto_id . " and bettype=" . $rs->f['bettype'] . " and subid=" . $rs->f['subid'] . " and numbers='" . $rs->f['numbers'] . "' and uperid" . $acl . "=" . $u_id;
            $q = getdb($sql);
            $amt += isset($q->f['amount']) ? $q->f['amount'] : "0";
        }
        if ($acl <= 5)
        {
            $sql = "Select SUM( amount " . ") as amount From bet2_{$this->tbl} Where lotto_id=" . $this->m_lotto_id . " and bettype=" . $rs->f['bettype'] . " and subid=" . $rs->f['subid'] . " and numbers='" . $rs->f['numbers'] . "' and user_id=" . $u_id;
            $q = getdb($sql);
            $q->f['amount'] = isset($q->f['amount']) ? $q->f['amount'] : "0";
            $amt -= $q->f['amount'];
        }
        return $amt;
    }

    public function getBet_sb($id)
    {
        $this->autoport = false;
        if ($this->GetConfig("protect_start_{$this->tbl}") !== "0" && $this->GetConfig("autoportmod_{$this->tbl}") !== "0")
        {
            $this->autoport = 1;
        }
        $bmin = floatval($this->GetConfig("betmin_{$this->tbl}"));
        $bmin2face = floatval($this->GetConfig("betmin2face_{$this->tbl}"));
        if ($bmin2face <= 0)
        {
            $bmin2face = $bmin;
        }
        if (0 < $id)
        {
            $rs = getdb("!!Select * From bet_{$this->tbl} Where IFNULL(state,0)=0 and id=" . $id . " and (rame=0 or rame=2) LIMIT 1");
        } else
        {
            $rs = getdb("!!Select * From bet_{$this->tbl} Where IFNULL(state,0)=0 and (rame=0 or rame=2)");
        }

        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            if (in_array($rs->f['bettype'], $this->g_2face_sb))
            {
                $this->betmin = $bmin2face;
            } else
            {
                $this->betmin = $bmin;
            }
            $this->m_lotto_id = $rs->f['lotto_id'];
            if ($this->bettotal_by_lotto_id)
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}_" . $this->m_lotto_id;
            } else if ($paperOpen = $this->getPaperOpen($rs->f['lotto_id']))
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}_{$paperOpen}";
            } else
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}";
            }
            $this->SetBetTotal($rs);
            $cmdStr = "Update bet_{$this->tbl} Set state=1 Where id=" . $rs->f['id'] . " LIMIT 1";
            getdb($cmdStr);
            $rs->next();
        }
    }

    public function SetBetTotal($sqlQ)
    {
        $lotto_id = $sqlQ->f['lotto_id'];
        $user_id = $sqlQ->f['user_id'];
        $disk = $sqlQ->f['disk'];
        $bettype = $sqlQ->f['bettype'];
        $subid = $sqlQ->f['subid'];
        $numbers = $sqlQ->f['numbers'];
        $rate = $sqlQ->f['rate'];
        $percent2 = $sqlQ->f['percent2'];
        $percent3 = $sqlQ->f['percent3'];
        $percent4 = $sqlQ->f['percent4'];
        $percent5 = $sqlQ->f['percent5'];
        $percent255 = $sqlQ->f['percent255'];
        $back1 = $sqlQ->f['back1'];
        $back2 = $sqlQ->f['back2'];
        $back3 = $sqlQ->f['back3'];
        $back4 = $sqlQ->f['back4'];
        $back5 = $sqlQ->f['back5'];
        $back6 = $sqlQ->f['back6'];
        $rame = $sqlQ->f['rame'] == 2 ? 0 - 1 : 1;
        $amount = $sqlQ->f['amount'] * $rame;
        $isgroup36 = false;
        $iscombination = false;
        $isR1Pass = false;
        $rec = 1;
        $nums = array();
        if ($bettype == 46)
        {
            $nums = explode(",", $numbers);
            $isR1Pass = true;
        } else if (11 <= strlen($numbers))
        {
            $iscombination = true;
            $s1 = substr($numbers, 0, strpos($numbers, ","));
            $buf = substr(strstr($numbers, ","), 1, strlen($numbers));
            $s2 = substr($buf, 0, strpos($buf, ","));
            $s3 = substr(strstr($buf, ","), 1, strlen($buf));
            $i = 0;
            while ($i < strlen($s1))
            {
                $j = 0;
                while ($j < strlen($s2))
                {
                    $k = 0;
                    while ($k < strlen($s3))
                    {
                        array_push($nums, substr($s1, $i, 1) . substr($s2, $j, 1) . substr($s3, $k, 1));
                        ++$k;
                    }
                    ++$j;
                }
                ++$i;
            }
        } else if (3 < strlen($numbers))
        {
            $i = 0;
            while ($i < strlen($numbers))
            {
                array_push($nums, substr($numbers, $i, 1));
                ++$i;
            }
            $isgroup36 = true;
        } else
        {
            $nums[0] = $numbers;
        }

        $rs = 0;
        if (( $rs = $this->GetAutoRateConfig($bettype, $numbers, $subid) ) && 0 < $rs->f['money'])
        {
            $i = 0;
            while ($i < count($nums))
            {
                $this->SetAutoRate($rs, $lotto_id, $bettype, $nums[$i], $amount * $percent255 / 100, $subid);
                ++$i;
            }
        }
        if ($iscombination)
        {
            $rate = round($rate * count($nums), 3);
            $amount = round($amount / count($nums), 3);
        } else if ($isR1Pass)
        {
            $rate = round(pow($rate, 0.33333), 3);
        }
        $cmdStr = "";
        $amount2 = 0;
        $amount4 = 0;
        $damount2 = 0;
        $fback2 = 0;
        $fback3 = 0;
        $lamt = 0;
        $acl = "";
        $sl = $this->GetUserID($user_id);
        $famt = 0;
        $famt_acl = array(0, 0, 0, 0, 0);
        $percent_ = array(0, 0, 0, 0, 0, 0);
        if ($this->autoport)
        {
            $rs3d = getdb("Select A.protmod from k_user_percent A Left Join k_user B ON B.id = A.user_id where A.user_id=" . $sl[3] . " and B.acl=5 and A.gametype = {$this->gametype}");
            if ($rs3d->f['protmod'] == 0)
            {
                $this->autoport = false;
            }
        }
        if (( $rss = $this->GetAutoRateConfigSS($bettype, $sl[3], $subid) ) && 0 < $rss->f['money'])
        {
            $i = 0;
            while ($i < count($nums))
            {
                $this->SetAutoRateSS($rss, $lotto_id, $bettype, $nums[$i], $amount * $percent5 / 100, $sl[3], $rate, $subid);
                ++$i;
            }
        }
        $percent_sw = 0;
        if ($this->autoport)
        {
            $i = 0;
            while ($i < count($sl) - 1)
            {
                $usr = get_user(intval($sl[$i]), $this->gametype);
                $percent_[$i + 1] = intval($usr['percent2']);
                ++$i;
            }
        }
        $i = 0;
        while ($i < count($sl))
        {
            $u_id = intval($sl[$i]);
            $famt = 0;
            $amount1 = array_sum($famt_acl) + $amount;
            $amount3 = round($amount1 * $rate, 3);
            $fback1 = round($amount1 * ( 100 - $back5 ) / 100, 3);
            switch ($i)
            {
                case 0 :
                    $amount2 = round($amount * $percent2 / 100, 3);
                    $damount2 = round($amount * $percent2 / 100, 3);
                    $fback2 = round(( $back2 - $back1 ) * $amount / 100, 3);
                    $fback3 = round($fback1 * $percent2 / 100, 3);
                    break;
                case 1 :
                    $amount2 = round($famt_acl[0] * $percent_[1] / 100 + $amount * $percent3 / 100, 3);
                    $damount2 = round($amount * $percent3 / 100, 3);
                    $fback2 = round(( $back3 - $back2 ) * $amount / 100, 3);
                    $fback3 = round($fback1 * $percent3 / 100, 3);
                    break;
                case 2 :
                    $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) / 100 + $famt_acl[1] * $percent_[2] / 100 + $amount * $percent4 / 100, 3);
                    $damount2 = round($amount * $percent4 / 100, 3);
                    $fback2 = round(( $back4 - $back3 ) * $amount / 100, 3);
                    $fback3 = round($fback1 * $percent4 / 100, 3);
                    break;
                case 3 :
                    $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) / 100 + $famt_acl[1] * round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) / 100 + $famt_acl[2] * $percent_[3] / 100 + $amount * $percent5 / 100, 3);
                    $damount2 = round($amount * $percent5 / 100, 3);
                    $fback2 = round(( $back5 - $back4 ) * $amount / 100, 3);
                    $fback3 = round($fback1 * $percent5 / 100, 3);
                    break;
                case 4 :
                    $amount2 = round($famt_acl[0] * ( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) - round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[1] * ( 100 - $percent_[2] - round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[2] * ( 100 - $percent_[3] ) / 100 + $famt_acl[3] + $amount * $percent255 / 100, 3);
                    $damount2 = round($amount * $percent255 / 100, 3);
                    $fback2 = 0;
                    $fback3 = round($fback1 * $percent255 / 100, 3);
                    break;
            }
            $acl = $i + 2;
            $lamt = $amount2;
            if ($this->autoport && $rame == 1 && !$iscombination)
            {
                $auto_nt = $this->GetUserAmounts($u_id, $bettype, "auto_nt", $subid);
            } else
            {
                $auto_nt = 0;
            }
            if (!$isgroup36 && !$iscombination && !$isR1Pass)
            {
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_3d} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$bettype} and subid={$subid} and numbers='{$nums[0]}'");
                if ($sqlBetTotal->eof)
                {
                    if ($this->autoport && 0 < $auto_nt && $acl < 6)
                    {
                        if ($auto_nt + $this->betmin < $lamt)
                        {
                            $famt = $lamt - $auto_nt;
                            $lamt = $auto_nt;
                            $amount1 -= $famt;
                            $amount3 = round($amount1 * $rate, 3);
                        }
                        if (0 < $famt)
                        {
                            ++$rec;
                        }
                    }
                    $amount4 = round($lamt * $rate, 3);
                    $cmdStr = "Insert Into {$this->bettotal_3d} " . "(lotto_id,user_id,bettype,subid,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d," . "updatetime" . ") Values (" . strval($lotto_id) . "," . strval($u_id) . "," . strval($bettype) . "," . strval($subid) . "," . "'" . $nums[0] . "'," . $rec . "," . strval($amount1) . "," . strval($lamt) . "," . strval($amount3) . "," . strval($amount4) . "," . strval($fback1) . "," . strval($fback2) . "," . strval($fback3) . "," . strval($rate) . "," . strval($rate);
                    $k = 1;
                    while ($k <= 4)
                    {
                        if ($disk == $k)
                        {
                            $cmdStr .= ",1," . strval($amount) . "," . strval($damount2) . "," . strval($amount3) . "," . strval($amount4) . "," . strval($fback1) . "," . strval($fback2) . "," . strval($fback3) . "," . strval($rate) . "," . strval($rate);
                        } else
                        {
                            $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        }
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                } else
                {
                    if ($this->autoport && 0 < $auto_nt && $acl < 6)
                    {
                        if ($auto_nt + $this->betmin < $sqlBetTotal->f['amount2'] + $lamt)
                        {
                            $famt = $sqlBetTotal->f['amount2'] + $lamt - $auto_nt;
                            $lamt = $auto_nt - $sqlBetTotal->f['amount2'];
                            $amount1 -= $famt;
                            $amount3 = round($amount1 * $rate, 3);
                        }
                        if (0 < $famt)
                        {
                            ++$rec;
                        }
                    }
                    $amount4 = round($lamt * $rate, 3);
                    $cmdStr = "Update {$this->bettotal_3d} Set " . "totalnum = totalnum +" . strval($rec * $rame) . ",amount1 = amount1 +" . strval($amount1) . ",amount2 = amount2 +" . strval($lamt) . ",amount3 = amount3 +" . strval($amount3) . ",amount4 = amount4 +" . strval($amount4) . ",back1 = back1 +" . strval($fback1) . ",back2 = back2 +" . strval($fback2) . ",back3 = back3 +" . strval($fback3) . ",rate1 = ((totalnum-1)*rate1 +" . strval($rate) . ")/(totalnum)" . ",rate2 = amount3 / amount1";
                    $cdisk = substr("abcd", $disk - 1, 1);
                    $cmdStr .= ",totalnum" . $cdisk . " = totalnum" . $cdisk . " + " . strval($rame) . ",amount1" . $cdisk . " = amount1" . $cdisk . " +" . strval($amount) . ",amount2" . $cdisk . " = amount2" . $cdisk . " +" . strval($damount2) . ",amount3" . $cdisk . " = amount3" . $cdisk . " +" . strval($amount3) . ",amount4" . $cdisk . " = amount4" . $cdisk . " +" . strval($amount4) . ",back1" . $cdisk . " = back1" . $cdisk . " +" . strval($fback1) . ",back2" . $cdisk . " = back2" . $cdisk . " +" . strval($fback2) . ",back3" . $cdisk . " = back3" . $cdisk . " +" . strval($fback3) . ",rate1" . $cdisk . " = ((totalnum" . $cdisk . "-1)*rate1" . $cdisk . " +" . strval($rate) . ")/(totalnum" . $cdisk . ")" . ",rate2" . $cdisk . " = amount3" . $cdisk . " / amount1" . $cdisk;
                    $cmdStr .= ",updatetime =" . systime() . " Where lotto_id=" . strval($lotto_id) . " and user_id=" . strval($u_id) . " and bettype=" . strval($bettype) . " and subid=" . strval($subid) . " and numbers='" . $nums[0] . "' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
            } else
            {
                $gamt = 0;
                if ($this->autoport && 0 < $auto_nt && ( $acl < $pacl && $pacl < 6 ))
                {
                    $gamt = $this->GetBetTotalFromAgentId($i + 2, $u_id, $sqlQ);
                    if ($auto_nt + $this->betmin < $gamt)
                    {
                        $famt = $gamt - $auto_nt;
                    }
                    if (0 < $famt)
                    {
                        ++$rec;
                    }
                }
                while ($j < count($nums))
                {
                    $sqlBetTotal = getdb("!!Select * From {$this->bettotal_3d} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$bettype} and subid={$subid} and numbers='{$nums[$j]}'");
                    if ($sqlBetTotal->eof)
                    {
                        if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin < $gamt && $acl < 6)
                        {
                            $lamt = $auto_nt;
                        }
                        $amount4 = round($lamt * $rate, 3);
                        $cmdStr = "Insert Into {$this->bettotal_3d} " . "(lotto_id,user_id,bettype,subid,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3 ,rate1 ,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d," . "updatetime" . ") Values ({$lotto_id},{$u_id},{$bettype},{$subid},'{$nums[$j]}',{$rec},{$amount1},{$lamt}," . "{$amount3},{$amount4},{$fback1},{$fback2},{$fback3},{$rate},{$rate}";
                        $k = 1;
                        while ($k <= 4)
                        {
                            if ($disk == $k)
                            {
                                $cmdStr .= ",1,{$amount},{$damount2}," . "{$amount3},{$amount4},{$fback1},{$fback2},{$fback3},{$rate},{$rate}";
                            } else
                            {
                                $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                            }
                            ++$k;
                        }
                        $cmdStr .= "," . systime();
                        $cmdStr .= ")";
                    } else
                    {
                        if ($this->autoport && 0 < $auto_nt && $acl < 6 && $auto_nt + $this->betmin < $gamt)
                        {
                            $lamt = $auto_nt - ( $gamt - $amount2 );
                        }
                        $amount4 = round($lamt * $rate, 3);
                        $cmdStr = "Update {$this->bettotal_3d} Set " . "totalnum = totalnum +" . strval($rec * $rame) . ",amount1 = amount1 +" . $amount1 . ",amount2 = amount2 +" . $lamt . ",amount3 = amount3 +" . $amount3 . ",amount4 = amount4 +" . $amount4 . ",back1 = back1 +" . $fback1 . ",back2 = back2 +" . $fback2 . ",back3 = back3 +" . $fback3 . ",rate1 = ((totalnum-1)*rate1 +" . $rate . ")/(totalnum)" . ",rate2 = amount3 / amount1";
                        $cdisk = substr("abcd", $disk - 1, 1);
                        $cmdStr .= ",totalnum" . $cdisk . " = totalnum" . $cdisk . " +" . $rame . ",amount1" . $cdisk . " = amount1" . $cdisk . " +" . $amount . ",amount2" . $cdisk . " = amount2" . $cdisk . " +" . $damount2 . ",amount3" . $cdisk . " = amount3" . $cdisk . " +" . $amount3 . ",amount4" . $cdisk . " = amount4" . $cdisk . " +" . $amount4 . ",back1" . $cdisk . " = back1" . $cdisk . " +" . $fback1 . ",back2" . $cdisk . " = back2" . $cdisk . " +" . $fback2 . ",back3" . $cdisk . " = back3" . $cdisk . " +" . $fback3 . ",rate1" . $cdisk . " = ((totalnum" . $cdisk . "-1)*rate1" . $cdisk . " +" . $rate . ")/(totalnum" . $cdisk . ")" . ",rate2" . $cdisk . " = amount3" . $cdisk . " / amount1" . $cdisk;
                        $cmdStr .= ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=" . $u_id . " and bettype=" . $bettype . " and subid=" . $subid . " and numbers='" . $nums[$j] . "' LIMIT 1";
                    }
                    if (!empty($cmdStr))
                    {
                        getdb($cmdStr);
                    }
                    ++$j;
                    break;
                }
            }
            if ($this->autoport && 0 < $auto_nt && 0 < $famt && $acl < 6)
            {
                $this->InsertBet2_3D($this->m_lotto_id, $sl, $acl, $famt, $sqlQ);
                $famt_acl[$i] = $famt;
            }
            ++$i;
        }
    }

    public function SetAutoRate($sqlAutoRate_Config, $lotto_id, $bettype, $numbers, $amount, $subid = 0)
    {
        $cmdStr = "";
        $famt = 0;
        $downrate = 0;
        $uprate = 0;
        $limitrate = $sqlAutoRate_Config->f['limitrate'];
        $bet = 0;
        $typeflag = $sqlAutoRate_Config->f['typeflag'];
        $sqlAutoRate = getdb("!!Select * From autorate_{$this->tbl} Where lotto_id = {$lotto_id} and bettype = {$bettype} and subid = {$subid} and numbers='{$numbers}'");
        if ($sqlAutoRate->eof)
        {
            $famt = $amount;
            $uamt = $famt;
            if ($sqlAutoRate_Config->f['money'] <= $famt)
            {
                $uamt = 0 < $sqlAutoRate_Config->f['money'] ? $famt % intval($sqlAutoRate_Config->f['money']) : 0;
            }
            $cmdStr = "Insert into autorate_{$this->tbl} (lotto_id,bettype,subid,numbers,amount,total) values ({$lotto_id},{$bettype},{$subid},'{$numbers}',{$uamt},{$famt}) " . "ON DUPLICATE KEY UPDATE amount = amount + {$uamt} , total = total + {$famt}";
        } else
        {
            $famt = $sqlAutoRate->f['amount'] + $amount;
            $uamt = $famt;
            if ($sqlAutoRate_Config->f['money'] <= $famt)
            {
                $uamt = 0 < $sqlAutoRate_Config->f['money'] ? $famt % intval($sqlAutoRate_Config->f['money']) : 0;
            }
            $cmdStr = "Update autorate_{$this->tbl} set amount = {$uamt},total = total + {$amount} Where lotto_id= {$lotto_id} AND bettype = {$bettype} AND subid = {$subid} AND numbers ='{$numbers}' LIMIT 1";
        }
        getdb($cmdStr);
        if ($sqlAutoRate_Config->f['money'] <= $famt)
        {
            if (0 < $sqlAutoRate_Config->f['money'])
            {
                $bet = intval($famt / $sqlAutoRate_Config->f['money']);
            } else
            {
                $bet = 1;
            }
            $downrate = $sqlAutoRate_Config->f['downrate'] * $bet;
            $uprate = $sqlAutoRate_Config->f['uprate'] * $bet;
            $this->ChangeRate("rate_{$this->tbl}", $lotto_id, $bettype, $numbers, $downrate, $uprate, $limitrate, $typeflag, $subid);
        }
    }

    public function ChangeRate($tblname, $lotto_id, $bettype, $numbers, $downrate, $uprate, $limitrate, $typeflag, $subid = 0)
    {
        $ratenumArr = array(13, 14, 15, 16, 20, 21, 35);
        $ratenumRateArr = array();
        $x1 = intval($numbers);
        $x2 = intval($numbers);
        $x2 = 1;
        if (in_array($bettype, $ratenumArr))
        {
            $sql = "SELECT r01, r02, r03, disk FROM rate_{$this->tbl} WHERE lotto_id = {$lotto_id} AND\tbettype = {$bettype} AND subid = {$subid} LIMIT 4";
            $rs = getdb($sql);
            $diskRateArr = array(array(0, 0, 0), array(0, 0, 0), array(0, 0, 0), array(0, 0, 0));
            if (in_array($bettype, array(20, 21, 35)))
            {
                $numLen = 3;
            } else
            {
                $numLen = 2;
            }
            while (!$rs->eof)
            {
                $diskRateArr[$rs->f[3]] = array($rs->f[0], $rs->f[1], $rs->f[2], $rs->f[3]);
                $rs->next();
            }
            if ($numLen == 2)
            {
                if (substr($numbers, 0, 1) == substr($numbers, 1, 1))
                {
                    $rateChkIdx = 1;
                } else
                {
                    $rateChkIdx = 0;
                }
            } else if ($numLen == 3)
            {
                if (substr($numbers, 0, 1) == substr($numbers, 1, 1) && substr($numbers, 0, 1) == substr($numbers, 2, 1))
                {
                    $rateChkIdx = 2;
                } else if (substr($numbers, 0, 1) == substr($numbers, 1, 1) || substr($numbers, 0, 1) == substr($numbers, 2, 1) || substr($numbers, 1, 1) == substr($numbers, 2, 1))
                {
                    $rateChkIdx = 1;
                } else
                {
                    $rateChkIdx = 0;
                }
            }
            $i = 1;
            while ($i < 5)
            {
                $rateChkVal = $diskRateArr[$i][$rateChkIdx];
                $okVal = min($downrate, $rateChkVal);
                $sql = "INSERT INTO `ratenum_{$this->tbl}` (lotto_id,\tbettype, subid,\tdisk,\tnum, rate)";
                $sql .= " VALUES ({$lotto_id} , {$bettype} , {$subid}, {$i}, {$x1}, {$okVal})";
                $sql .= " ON DUPLICATE KEY UPDATE rate = IF(rate + {$downrate} > {$rateChkVal}, {$rateChkVal}, rate + {$downrate})";
                getdb($sql);
                ++$i;
            }
        } else
        {
            $x1Idx = "r" . str_pad($x1, 2, "0", STR_PAD_LEFT);
            $x2Idx = "r" . str_pad($x2, 2, "0", STR_PAD_LEFT);
            $sqlRate = getdb("!!Select * From {$tblname} Where lotto_id={$lotto_id} AND bettype={$bettype} AND subid = {$subid} AND disk = 1 LIMIT 1");
            if (!$sqlRate->eof)
            {
                $f = $sqlRate->f;
                switch ($typeflag)
                {
                    case 1 :
                        if ($limitrate < $f[$x1Idx] - $downrate && $limitrate < $f[$x2Idx] - $downrate)
                        {
                            $sql = "UPDATE {$tblname} A LEFT JOIN {$tblname} B ON (A.`bettype` = B.`bettype` AND A.`subid` = B.`subid` AND A.`disk` = B.`disk`)";
                            $sql .= " SET A.{$x1Idx} = A.{$x1Idx} - {$downrate}, A.{$x2Idx} = IF((A.{$x2Idx} + {$uprate}) > B.{$x2Idx}, B.{$x2Idx}, (A.{$x2Idx} + {$uprate}))";
                            $sql .= " WHERE A.lotto_id = {$lotto_id} AND A.bettype = {$bettype} AND A.subid = {$subid} AND B.`lotto_id` = 0 ";
                            getdb($sql);
                        } else if ($limitrate < min($f[$x1Idx], $f[$x2Idx]))
                        {
                            $f[$x2Idx] = $limitrate + ( $f[$x1Idx] - $f[$x2Idx] ) + $uprate * 2;
                            $sql = "UPDATE {$tblname} A LEFT JOIN {$tblname} B ON (A.`bettype` = B.`bettype` AND A.`subid` = B.`subid` AND A.`disk` = B.`disk`)";
                            $sql .= " SET A.{$x1Idx} = IF({$limitrate} > B.{$x1Idx}, B.{$x1Idx}, {$limitrate}), A.{$x2Idx} = IF({$f[$x2Idx]} > B.{$x2Idx}, B.{$x2Idx}, {$f[$x2Idx]})";
                            $sql .= " WHERE A.lotto_id = {$lotto_id} AND A.bettype = {$bettype} AND A.subid = {$subid} AND B.`lotto_id` = 0 ";
                            getdb($sql);
                        }
                        break;
                    default :
                        if ($limitrate < $f[$x1Idx] - $downrate)
                        {
                            getdb("update {$tblname} set {$x1Idx} = {$x1Idx} - {$downrate} Where lotto_id={$lotto_id} AND bettype={$bettype} AND subid = {$subid}");
                        } else
                        {
                            getdb("update {$tblname} set {$x1Idx} = {$limitrate} Where lotto_id={$lotto_id} AND bettype={$bettype} AND subid = {$subid}");
                        }
                        break;
                }
            }
        }
    }

    public function SetAutoRateSS($sqlAutoRate_Config, $lotto_id, $bettype, $numbers, $amount, $user_id, $rate, $subid = 0)
    {
        $cmdStr = "";
        $famt = 0;
        $downrate = 0;
        $uprate = 0;
        $limitrate = $sqlAutoRate_Config->f['limitrate'];
        $bet = 0;
        $typeflag = $sqlAutoRate_Config->f['typeflag'];
        $sqlAutoRate = getdb("!!Select * From autorate_ss Where gametype={$this->gametype} AND lotto_id = {$lotto_id} AND user_id = {$user_id} and bettype = {$bettype} and subid = {$subid} and numbers='{$numbers}'");
        if ($sqlAutoRate->eof)
        {
            $famt = $amount;
            $uamt = $famt;
            if ($sqlAutoRate_Config->f['money'] <= $famt)
            {
                $uamt = 0 < $sqlAutoRate_Config->f['money'] ? $famt % intval($sqlAutoRate_Config->f['money']) : 0;
            }
            $cmdStr = "Insert into autorate_ss (lotto_id,bettype,subid,numbers,amount,total,gametype,user_id) values ({$lotto_id},{$bettype},{$subid},'{$numbers}',{$uamt},{$famt},{$this->gametype},{$user_id}) " . "ON DUPLICATE KEY UPDATE amount = amount + {$uamt} , total = total + {$famt}";
        } else
        {
            $famt = $sqlAutoRate->f['amount'] + $amount;
            $uamt = $famt;
            if ($sqlAutoRate_Config->f['money'] <= $famt)
            {
                $uamt = 0 < $sqlAutoRate_Config->f['money'] ? $famt % intval($sqlAutoRate_Config->f['money']) : 0;
            }
            $cmdStr = "Update autorate_ss set amount = {$uamt},total = total + {$amount} Where lotto_id= {$lotto_id} AND gametype={$this->gametype} and user_id = {$user_id} AND bettype = {$bettype} AND subid = {$subid} AND numbers ='{$numbers}' LIMIT 1";
        }
        getdb($cmdStr);
        if ($sqlAutoRate_Config->f['money'] <= $famt)
        {
            if (0 < $sqlAutoRate_Config->f['money'])
            {
                $bet = intval($famt / $sqlAutoRate_Config->f['money']);
            } else
            {
                $bet = 1;
            }
            $downrate = $sqlAutoRate_Config->f['downrate'] * $bet;
            $uprate = 0;
            $this->ChangeRateSS("rates_{$this->tbl}", $lotto_id, $bettype, $numbers, $downrate, $uprate, $limitrate, $typeflag, $user_id, $rate, $subid);
        }
    }

    public function ChangeRateSS($tblname, $lotto_id, $bettype, $numbers, $downrate, $uprate, $limitrate, $typeflag, $user_id, $rate, $subid = 0)
    {
        $ratenumArr = array(13, 14, 15, 16, 20, 21, 35);
        $x1 = intval($numbers);
        if (in_array($bettype, $ratenumArr))
        {
            $sql = "INSERT INTO `ratenums_{$this->tbl}` (lotto_id,\tbettype, subid,\tuser_id,\tnum, rate)";
            $sql .= " VALUES ({$lotto_id} , {$bettype} , {$subid}, {$user_id}, {$x1},{$downrate})";
            $sql .= " ON DUPLICATE KEY UPDATE rate = rate + {$downrate}";
            getdb($sql);
        } else
        {
            $x1Idx = "r" . str_pad($x1, 2, "0", STR_PAD_LEFT);
            $sql = "INSERT INTO `{$tblname}` (lotto_id,\tbettype, subid,\tuser_id,\t{$x1Idx})";
            $sql .= " VALUES ({$lotto_id} , {$bettype} , {$subid}, {$user_id}, {$downrate})";
            $sql .= " ON DUPLICATE KEY UPDATE {$x1Idx} = {$x1Idx} + {$downrate}";
            getdb($sql);
        }
    }

    public function InsertBet2_3D($lotto_id, $users, $acl, $amount, $sqlBet)
    {
        $rate = 0;
        $rate5 = 0;
        $rate = $this->GetCurrectRate(1, $acl < 5 ? $users[3] : 0, $this->m_lotto_id, $sqlBet->f['bettype'], (boolean) $sqlBet->f['numbers'], $sqlBet->f['subid']);
        if ($acl < 5)
        {
            $rate5 = $this->GetCurrectRate(1, 0, $this->m_lotto_id, $sqlBet->f['bettype'], (boolean) $sqlBet->f['numbers'], $sqlBet->f['subid']);
            $rate5 = $rate5 ? $rate5 - $rate : 0;
        } else
        {
            $rate5 = 0;
        }
        $percent_sw = 0;
        $bettype = $sqlBet->f['bettype'];
        $numbers = $sqlBet->f['numbers'];
        $line_nums = $sqlBet->f['line_nums'];
        $subid = $sqlBet->f['subid'];
        switch ($acl)
        {
            case 2 :
                $u2 = get_user($users[0], $this->gametype);
                $u3 = get_user($users[1], $this->gametype);
                $u4 = get_user($users[2], $this->gametype);
                $u5 = get_user($users[3], $this->gametype);
                $uperid3 = $u3['id'];
                $uperid4 = $u4['id'];
                $uperid5 = $u5['id'];
                $percent3 = $u2['percent2'];
                $percent4 = round(( 100 - $percent3 ) * $u3['percent2'] / 100, 0);
                $percent5 = round(( 100 - $percent3 - $percent4 ) * $u4['percent2'] / 100, 0);
                $back2 = $this->GetBackArr($u2['id'], $bettype, $subid, 1, 2);
                $back3 = $this->GetBackArr($u3['id'], $bettype, $subid, 1, 3);
                $back4 = $this->GetBackArr($u4['id'], $bettype, $subid, 1, 4);
                $back5 = $this->GetBackArr($u5['id'], $bettype, $subid, 1, 5);
                $back6 = $this->GetBackArr($u5['supe_account'], $bettype, $subid, 1, 255);
                break;
            case 3 :
                $u3 = get_user($users[1], $this->gametype);
                $u4 = get_user($users[2], $this->gametype);
                $u5 = get_user($users[3], $this->gametype);
                $uperid3 = $u3['id'];
                $uperid4 = $u4['id'];
                $uperid5 = $u5['id'];
                $percent3 = 0;
                $percent4 = $u3['percent2'];
                $percent5 = round(( 100 - $percent4 ) * $u4['percent2'] / 100, 0);
                $back2 = 100;
                $back3 = $this->GetBackArr($u3['id'], $bettype, $subid, 1, 3);
                $back4 = $this->GetBackArr($u4['id'], $bettype, $subid, 1, 4);
                $back5 = $this->GetBackArr($u5['id'], $bettype, $subid, 1, 5);
                $back6 = $this->GetBackArr($u5['supe_account'], $bettype, $subid, 1, 255);
                break;
            case 4 :
                $u4 = get_user($users[2], $this->gametype);
                $u5 = get_user($users[3], $this->gametype);
                $uperid3 = 0;
                $uperid4 = $u4['id'];
                $uperid5 = $u5['id'];
                $percent3 = 0;
                $percent4 = 0;
                $percent5 = $u4['percent2'];
                $back2 = 100;
                $back3 = 100;
                $back4 = $this->GetBackArr($u4['id'], $bettype, $subid, 1, 4);
                $back5 = $this->GetBackArr($u5['id'], $bettype, $subid, 1, 5);
                $back6 = $this->GetBackArr($u5['supe_account'], $bettype, $subid, 1, 255);
                break;
            case 5 :
                $u5 = get_user($users[3], $this->gametype);
                $uperid3 = 0;
                $uperid4 = 0;
                $uperid5 = $u5['id'];
                $percent3 = 0;
                $percent4 = 0;
                $percent5 = 0;
                $back2 = 100;
                $back3 = 100;
                $back4 = 100;
                $back5 = $this->GetBackArr($u5['id'], $bettype, $subid, 1, 5);
                $back6 = $this->GetBackArr($u5['supe_account'], $bettype, $subid, 1, 255);
                break;
        }
        $percent255 = max(100 - $percent5 - $percent4 - $percent3, 0);
        $betmode = 1;
        $rame = 0;
        $tempAcl = $acl - 2;
        $tempAcl2 = $tempAcl + 1;
        $sqlQ = getdb("INSERT INTO bet2_{$this->tbl} " . "(user_id,prev_id,uperid3,uperid4,uperid5,lotto_id,createtime,acl," . "bettype,subid,numbers,line_nums,amount,unitmoney,rate,rate2,rate3,rate4,rate5," . "a2,b2,r2,a3,b3,c3,d3,e3,r3,a4,b4,c4,d4,e4,r4,a5,b5,c5,d5," . "e5,g5,r5,r6," . "percent3,percent4,percent5,percent255,back2,back3,back4,back5,back6,betmode," . "rame,state) VALUES (" . "{$users[$tempAcl]},{$users[$tempAcl2]}," . $uperid3 . "," . $uperid4 . "," . $uperid5 . ",{$lotto_id}," . systime() . ",{$acl},{$bettype},{$subid},'" . $numbers . "'," . "'" . $line_nums . "',{$amount},{$amount},{$rate}, 0, 0, 0, {$rate5}, 0, 0, 0, 0, 0, 0, 0, 0," . " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0," . "{$percent3},{$percent4},{$percent5},{$percent255},{$back2},{$back3},{$back4},{$back5},{$back6},{$betmode},{$rame},1)");
    }

    public function getBet2_3D($id)
    {
        $this->autoport = false;
        if ($this->GetConfig("protect_start_{$this->tbl}") !== "0" && $this->GetConfig("autoportmod_{$this->tbl}") !== "0")
        {
            $this->autoport = true;
        }
        $bmin = floatval($this->GetConfig("betmin_{$this->tbl}"));
        $bmin2face = floatval($this->GetConfig("betmin2face_{$this->tbl}"));
        if ($bmin2face <= 0)
        {
            $bmin2face = $bmin;
        }
        if (0 < $id)
        {
            $sqlBet2 = getdb("!!Select * From bet2_{$this->tbl} Where id = {$id} AND IFNULL(state,0)=0 and (rame=0 or rame=2)");
        } else
        {
            $sqlBet2 = getdb("!!Select * From bet2_{$this->tbl} Where IFNULL(state,0)=0 and (rame=0 or rame=2)");
        }
        $this->m_lotto_id = 0;
        while (!$sqlBet2->eof && !$sqlBet2->eof)
        {
            if (in_array($sqlBet2->f['bettype'], $this->g_2face_sb))
            {
                $this->betmin = $bmin2face;
            } else
            {
                $this->betmin = $bmin;
            }
            $this->m_lotto_id = $sqlBet2->f['lotto_id'];
            if ($this->bettotal_by_lotto_id)
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}_" . $this->m_lotto_id;
            } else if ($paperOpen = $this->getPaperOpen($sqlBet2->f['lotto_id']))
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}_{$paperOpen}";
            } else
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}";
            }
            $this->SetBet2Total($sqlBet2);
            $cmdStr = "Update bet2_{$this->tbl} Set state=1 Where id=" . $sqlBet2->f['id'] . " LIMIT 1";
            getdb($cmdStr);
            $sqlBet2->next();
        }
    }

    public function SetBet2Total($sqlQ)
    {
        $lotto_id = $sqlQ->f['lotto_id'];
        $user_id = $sqlQ->f['user_id'];
        $acl = $sqlQ->f['acl'];
        $bettype = $sqlQ->f['bettype'];
        $subid = $sqlQ->f['subid'];
        $numbers = $sqlQ->f['numbers'];
        $rate = $sqlQ->f['rate'];
        $percent3 = $sqlQ->f['percent3'];
        $percent4 = $sqlQ->f['percent4'];
        $percent5 = $sqlQ->f['percent5'];
        $percent255 = $sqlQ->f['percent255'];
        $back2 = $sqlQ->f['back2'];
        $back3 = $sqlQ->f['back3'];
        $back4 = $sqlQ->f['back4'];
        $back5 = $sqlQ->f['back5'];
        $back6 = $sqlQ->f['back6'];
        $rame = $sqlQ->f['rame'] == 2 ? 0 - 1 : 1;
        $amount = $sqlQ->f['amount'] * $rame;
        $isgroup36 = false;
        $isR1Pass = false;
        $rec = 1;
        $isowner = 1;
        $nums = array();
        if ($bettype == 46)
        {
            $nums = explode(",", $numbers);
            $isR1Pass = true;
        } else if (3 < strlen($numbers))
        {
            $i = 0;
            while ($i < strlen($numbers))
            {
                array_push($nums, substr($numbers, $i, 1));
                ++$i;
            }
            $isgroup36 = true;
        } else
        {
            array_push($nums, $numbers);
        }
        $rs = 0;
        if (( $rs = $this->GetAutoRateConfig($bettype, $numbers, $subid) ) && 0 < $rs->f['money'])
        {
            $i = 0;
            while ($i < count($nums))
            {
                $this->SetAutoRate($rs, $lotto_id, $bettype, $nums[$i], $amount * $percent255 / 100, $subid);
                ++$i;
            }
        }
        if ($isR1Pass)
        {
            $rate = round(pow($rate, 0.33333), 3);
        }
        $cmdStr = "";
        $amount2 = 0;
        $amount1 = 0;
        $amount4 = 0;
        $fback2 = 0;
        $fback3 = 0;
        $lamt = 0;
        $famt = 0;
        $pacl = "";
        $famt_acl = array(0, 0, 0, 0, 0);
        $percent_ = array(0, 0, 0, 0, 0, 0);
        $sl = array();
        $i = 2;
        while ($i <= 6)
        {
            if ($i < $acl)
            {
                array_push($sl, 0);
            } else if ($i == $acl)
            {
                array_push($sl, $user_id);
            } else if ($i == 6)
            {
                array_push($sl, 0);
            } else
            {
                array_push($sl, $sqlQ->f["uperid" . $i]);
            }
            ++$i;
        }
        if ($acl < 5 && ( $rss = $this->GetAutoRateConfigSS($bettype, $sl[3], $subid) ) && 0 < $rss->f['money'])
        {
            $i = 0;
            while ($i < count($nums))
            {
                $this->SetAutoRateSS($rss, $lotto_id, $bettype, $nums[$i], $amount * $percent5 / 100, $sl[3], $rate, $subid);
                ++$i;
            }
        }
        $percent_sw = 0;
        if ($this->autoport)
        {
            $i = 0;
            while ($i < count($sl) - 1)
            {
                $usr = get_user(intval($sl[$i]), $this->gametype);
                $percent_[$i + 1] = intval($usr['percent2']);
                ++$i;
            }
        }
        for ($i = 0; $i < count($sl); $i++)
        {
            $famt = 0;
            $u_id = $sl[$i];
            if ($u_id == 0 && $i + 2 < $acl)
            {
                continue;
            }
            if ($i + 2 == $acl)
            {
                $isowner = 0 - 1;
                $amount1 = $amount;
                $amount3 = round($amount1 * $rate, 0);
                $fback1 = round($amount1 * ( 100 - $back5 ) / 100, 3);
                $amount2 = $amount;
                $fback2 = $fback1;
                $fback3 = 0;
            } else
            {
                $isowner = 1;
                $amount1 = array_sum($famt_acl) + $amount;
                $amount3 = round($amount1 * $rate, 0);
                $fback1 = round($amount1 * ( 100 - $back5 ) / 100, 3);
                switch ($i)
                {
                    case 1 :
                        $amount2 = round($famt_acl[0] * $percent_[1] / 100 + $amount * $percent3 / 100, 3);
                        $fback2 = round(( $back3 - $back2 ) * $amount / 100, 3);
                        $fback3 = round($fback1 * $percent3 / 100, 3);
                        break;
                    case 2 :
                        $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) / 100 + $famt_acl[1] * $percent_[2] / 100 + $amount * $percent4 / 100, 3);
                        $fback2 = round(( $back4 - $back3 ) * $amount / 100, 3);
                        $fback3 = round($fback1 * $percent4 / 100, 3);
                        break;
                    case 3 :
                        $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) / 100 + $famt_acl[1] * round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) / 100 + $famt_acl[2] * $percent_[3] / 100 + $amount * $percent5 / 100, 3);
                        $fback2 = round(( $back5 - $back4 ) * $amount / 100, 3);
                        $fback3 = round($fback1 * $percent5 / 100, 3);
                        break;
                    case 4 :
                        $amount2 = round($famt_acl[0] * ( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) - round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[1] * ( 100 - $percent_[2] - round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[2] * ( 100 - $percent_[3] ) / 100 + $famt_acl[3] + $amount * $percent255 / 100, 3);
                        $fback2 = 0;
                        $fback3 = round($fback1 * $percent255 / 100, 3);
                        break;
                }
            }
            $pacl = $i + 2;
            $lamt = $amount2;
            if ($this->autoport && 0 < $lamt && ( $acl < $pacl && $pacl < 6 ) && $rame == 1)
            {
                $auto_nt = $this->GetUserAmounts($u_id, $bettype, "auto_nt", $subid);
            } else
            {
                $auto_nt = 0;
            }
            if (!$isgroup36 && !$isR1Pass)
            {
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_3d} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$bettype} and subid={$subid} and numbers='{$nums[0]}'");
                if ($sqlBetTotal->eof)
                {
                    if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin < $sqlBetTotal->f['amount2'] + $lamt && ( $acl < $pacl && $pacl < 6 ))
                    {
                        $lamt = $auto_nt;
                    }
                    $amount4 = round($lamt * $rate, 0);
                    $cmdStr = "Insert Into {$this->bettotal_3d} " . "(lotto_id,user_id,bettype,subid,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3 ,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d," . "updatetime" . ") Values (" . $lotto_id . "," . $u_id . "," . $bettype . "," . $subid . "," . "'" . $nums[0] . "'," . $rec . "," . $amount1 * $isowner . "," . $lamt * $isowner . "," . $amount3 * $isowner . "," . $amount4 * $isowner . "," . $fback1 . "," . $fback2 . "," . $fback3 . "," . $rate . "," . $rate;
                    $k = 1;
                    while ($k <= 4)
                    {
                        $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                } else
                {
                    if (0 < $auto_nt && ( $acl < $pacl && $pacl < 6 ))
                    {
                        if ($auto_nt + $this->betmin < $sqlBetTotal->f['amount2'] + $lamt)
                        {
                            $famt = $sqlBetTotal->f['amount2'] + $lamt - $auto_nt;
                            $lamt = $auto_nt - $sqlBetTotal->f['amount2'];
                        }
                        if (0 < $famt)
                        {
                            ++$rec;
                        }
                    }
                    $amount4 = round($lamt * $rate, 0);
                    $cmdStr = "Update {$this->bettotal_3d} Set totalnum = totalnum +" . strval($rec * $rame) . ",amount1 = amount1 +" . $amount1 * $isowner . ",amount2 = amount2 +" . $lamt * $isowner . ",amount3 = amount3 +" . $amount3 * $isowner . ",amount4 = amount4 +" . $amount4 * $isowner . ",back1 = back1 +{$fback1}" . ",back2 = back2 +{$fback2}" . ",back3 = back3 +{$fback3}" . ",rate1 = ((totalnum-1)*rate1 +{$rate} )/(totalnum)" . ",rate2 = amount3 / amount1" . ",updatetime =" . systime() . " Where lotto_id={$lotto_id}" . " and user_id={$u_id}" . " and bettype={$bettype}" . " and subid = {$subid}" . " and numbers='{$nums[0]}' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
            } else
            {
                $gamt = 0;
                if ($this->autoport && 0 < $auto_nt && ( $acl < $pacl && $pacl < 6 ))
                {
                    $gamt = $this->GetBetTotalFromAgentId($i + 2, $u_id, $sqlQ);
                    if ($auto_nt + $this->betmin < $gamt)
                    {
                        $famt = $gamt - $auto_nt;
                    }
                    if (0 < $famt)
                    {
                        ++$rec;
                    }
                }
                $j = 0;
                while ($j < count($nums))
                {
                    $sqlBetTotal = getdb("!!Select * From {$this->bettotal_3d} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$bettype} and subid = {$subid} and numbers='{$nums[$j]}'");
                    if ($sqlBetTotal->eof)
                    {
                        if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin < $gamt && ( $acl < $pacl && $pacl < 6 ))
                        {
                            $lamt = $auto_nt;
                        }
                        $amount4 = round($lamt * $rate, 0);
                        $cmdStr = "Insert Into {$this->bettotal_3d} " . "(lotto_id,user_id,bettype,subid,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3 ,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d," . "updatetime" . ") Values (" . $lotto_id . "," . $u_id . "," . $bettype . "," . $subid . "," . "'" . $nums[$j] . "'," . $rec . "," . $amount1 * $isowner . "," . $lamt * $isowner . "," . $amount3 * $isowner . "," . $amount4 * $isowner . "," . $fback1 . "," . $fback2 . "," . $fback3 . "," . $rate . "," . $rate;
                        $k = 1;
                        while ($k <= 4)
                        {
                            $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                            ++$k;
                        }
                        $cmdStr .= "," . systime();
                        $cmdStr .= ")";
                    } else
                    {
                        if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin < $gamt && ( $acl < $pacl && $pacl < 6 ))
                        {
                            $lamt = $auto_nt - ( $gamt - $amount2 );
                        }
                        $amount4 = round($lamt * $rate, 0);
                        $cmdStr = "Update {$this->bettotal_3d} Set " . "totalnum = totalnum +" . strval($rec * $rame) . ",amount1 = amount1 +" . $amount1 * $isowner . ",amount2 = amount2 +" . $lamt * $isowner . ",amount3 = amount3 +" . $amount3 * $isowner . ",amount4 = amount4 +" . $amount4 * $isowner . ",back1 = back1 +" . $fback1 . ",back2 = back2 +" . $fback2 . ",back3 = back3 +" . $fback3 . ",rate1 = ((totalnum-1)*rate1 +" . $rate . ")/(totalnum)" . ",rate2 = amount3 / amount1" . ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=" . $u_id . " and bettype=" . $bettype . " and subid=" . $subid . " and numbers='" . $nums[$j] . "' LIMIT 1";
                    }
                    getdb($cmdStr);
                    ++$j;
                    break;
                }
            }
            if ($this->autoport && 0 < $auto_nt && 0 < $famt && ( $acl < $pacl && $pacl < 6 ))
            {
                $this->InsertBet2_3D($this->m_lotto_id, $sl, $pacl, $famt, $sqlQ);
                $famt_acl[$i] = $famt;
            }
        }
    }

    public function CheckLottoOpen($id = "")
    {
        $ilotto_id = $id == "" ? 0 : $id;
        if ($ilotto_id == 0)
        {
            $q = getdb("!!Select * From lotto_{$this->tbl} Where state=1 and n3>=0 and n2>=0 and n1>=0 ORDER BY id LIMIT 1");
        } else
        {
            $q = getdb("!!Select * From lotto_{$this->tbl} Where id={$ilotto_id} AND state=1 and n3>=0 and n2>=0 and n1>=0 LIMIT 1");
        }
        $ilotto_id = $q->f['id'];
        $this->on3 = $q->f['n3'];
        $this->on2 = $q->f['n2'];
        $this->on1 = $q->f['n1'];
		$this->sb = $q->f['sb'];
		$this->dz = $q->f['dz'];
		$this->bz = $q->f['bz'];
		$this->bs = $q->f['bs'];
		$this->zd = intval($this->on1)+intval($this->on2)+intval($this->on3);
		//$arr=array($this->on1,$this->on2,$this->on3);
		//$len=count(array_count_values($arr));
		//$this->dz=($len<=2?1:2);
		$this->openDate = $q->f['open'];
        return $ilotto_id;
    }

    public function OpenProcess($lotto_id, $x = "")
    {
        if (!$this->ClearBetData($lotto_id, $x))
        {
            return 0;
        }
        $this->oamount = 0;
        $this->omb = 0;
        $this->oad = 0;
        $this->oua = 0;
        $this->our = 0;
        $this->ogg = 0;
        $this->CalcBetOpen($lotto_id, $x);
        $this->CalcBet2Open($lotto_id, $x);
        $this->CalcBet8Open($lotto_id, $x);
        $this->oad -= $this->ogg;
        $strCmd = "Update lotto_{$this->tbl} Set amount=" . $this->oamount . ",mb=" . $this->omb . ",ad=" . $this->oad . ",ua=" . $this->oua . ",ur=" . $this->our . ",rsum=" . ( $this->oad + $this->our ) . ",state = 2" . " Where id=" . $lotto_id;
		getdb($strCmd);
        return 1;
    }

    public function ClearBetData($lotto_id, $x = "")
    {
        $q = getdb("!!Select id From {$x}bet_{$this->tbl} Where lotto_id={$lotto_id} LIMIT 1");
        if ($q->eof)
        {
            getdb("Update lotto_{$this->tbl} Set state=2 Where id={$lotto_id}");
            return 0;
        }
        $strCmd = "";
        $strCmd = "Update {$x}bet_{$this->tbl} set a1=0,b1=0,a2=0,b2=0,c2=0,d2=0,a3=0,b3=0,c3=0,d3=0,a4=0,b4=0,c4=0,d4=0" . ",a5=0,b5=0,c5=0,d5=0,e5=0,g5=0,r5=0,r6=0,rame=0 Where lotto_id={$lotto_id} AND (rame=0 or rame=9)";
        getdb($strCmd);
        $strCmd = "Update {$x}bet2_{$this->tbl} set a2=0,b2=0,a3=0,b3=0,c3=0,d3=0,a4=0,b4=0,c4=0,d4=0" . ",a5=0,b5=0,c5=0,d5=0,e5=0,g5=0,r5=0,r6=0,rame=0 Where lotto_id={$lotto_id} AND (rame=0 or rame=9)";
        getdb($strCmd);
        $strCmd = "Update {$x}bet8_{$this->tbl} set r5=0,rame=0 Where lotto_id={$lotto_id} AND (rame=0 or rame=9)";
        getdb($strCmd);
        if ($this->tbl == "3d" || $this->tbl == "3t")
        {
            $calcbetStr = "";
        } else
        {
            $calcbetStr = "calcbet=0,";
        }
        $strCmd = "Update lotto_{$this->tbl} Set {$calcbetStr} amount=0,mb=0,ad=0,ua=0,ur=0,rsum=0,state=0 Where id={$lotto_id}";
        getdb($strCmd);
        if ($x == "")
        {
            $strCmd = "DELETE FROM xbet_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
            $strCmd = "DELETE FROM xbet2_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
            $strCmd = "DELETE FROM xbet8_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
        }
        return true;
    }

    public function CalcBetOpen($lotto_id, $x = "")
    {
		$strTemp = "";
        $minamt = $this->GetConfig("minbackbet_{$this->tbl}");
		$userdata=array(); //SELECT (case when b1>0 then (b1+amount) else a1 end) FROM `bet_3s3`
        $q = getdb("!!Select * From {$x}bet_{$this->tbl} Where lotto_id={$lotto_id} AND rame=0");

        while (!$q->eof)
        {
            $amount = $q->f['amount'];
            $a1 = $minamt <= $amount ? ( 100 - $q->f['back1'] ) / 100 * $amount : 0;
            $CalcBetAmountVal = $this->CalcBetAmount($q);
            if ($CalcBetAmountVal === false)
            {
                $errdoc = "({$this->tbl}){$strTemp}\xB9{獎時異常\x`單（I:" . $q->f['id'] . " B:" . $q->f['bettype'] . " Subid:" . $q->f['subid'] . " N:" . $q->f['numbers'] . " R:" . $q->f['rate'] . " A:" . $q->f['amount'] . "）";
                write_exceptional_log($q->f['user_id'], 1, $q->f['id'], $errdoc);
                $q->next();
                continue;
            }
            if ($q->f['rate'] < 1)
            {
                getdb("UPDATE {$x}bet_{$this->tbl} set a1=0,b1=0,a2=0,b2=0,c2=0,d2=0,a3=0,b3=0,c3=0,d3=0,a4=0,b4=0,c4=0,d4=0" . ",a5=0,b5=0,c5=0,d5=0,e5=0,g5=0,r5=0, rame=3 WHERE {$x}id = " . $q->f["{$x}id"] . " LIMIT 1");

                $errdoc = "({$this->tbl}){$strTemp}\xB9{獎時異常\x`單（I:" . $q->f['id'] . " B:" . $q->f['bettype'] . " Subid:" . $q->f['subid'] . " N:" . $q->f['numbers'] . " R:" . $q->f['rate'] . " A:" . $q->f['amount'] . "）";
                write_exceptional_log($q->f['user_id'], 1, $q->f['id'], $errdoc);
                $q->next();
                continue;
            }
            if ($CalcBetAmountVal === 0)
            {
                $this->oamount += $amount;
                getdb("UPDATE {$x}bet_{$this->tbl} set a1=0,b1=0,a2=0,b2=0,c2=0,d2=0,a3=0,b3=0,c3=0,d3=0,a4=0,b4=0,c4=0,d4=0" . ",a5=0,b5=0,c5=0,d5=0,e5=0,g5=0,r5=0, rame=9 WHERE {$x}id = " . $q->f["{$x}id"] . " LIMIT 1");
                $q->next();
                continue;
            }
            $b1 = $CalcBetAmountVal + $a1;
            $a2 = $minamt <= $amount ? ( $q->f['back1'] - $q->f['back2'] ) / 100 * $amount : 0;
            $b2 = $b1 + $a2;
            $c2 = $b2 * $q->f['percent2'] / 100;
            $d2 = $b2 * ( 100 - $q->f['percent2'] ) / 100;
            $a3 = $minamt <= $amount ? ( $q->f['back2'] - $q->f['back3'] ) / 100 * $amount : 0;
            $b3 = $b2 + $a3;
            $c3 = $b3 * $q->f['percent3'] / 100;
            $d3 = $b3 * ( 100 - $q->f['percent2'] - $q->f['percent3'] ) / 100;
            $r2 = $amount * $q->f['percent3'] / 100;
            $a3 -= $a3 * $q->f['percent2'] / 100;
            $a4 = $minamt <= $amount ? ( $q->f['back3'] - $q->f['back4'] ) / 100 * $amount : 0;
            $b4 = $b3 + $a4;
            $c4 = $b4 * $q->f['percent4'] / 100;
            $d4 = $b4 * ( 100 - $q->f['percent2'] - $q->f['percent3'] - $q->f['percent4'] ) / 100;
            $r3 = $amount * $q->f['percent4'] / 100;
            $a4 -= $a4 * ( $q->f['percent3'] + $q->f['percent2'] ) / 100;
            $e5 = $CalcBetAmountVal <= 0 ? 0 : $q->f['amount'] * $q->f['rate5'];
            $a5 = $minamt <= $amount ? ( $q->f['back4'] - $q->f['back5'] ) / 100 * $amount : 0;
            $b5 = $b4 + $a5 + $e5;
            $c5 = $b5 * $q->f['percent5'] / 100;
            $r4 = $amount * $q->f['percent5'] / 100;
            $a5 -= $a5 * ( $q->f['percent4'] + $q->f['percent3'] + $q->f['percent2'] ) / 100;
            $e5 -= $e5 * ( $q->f['percent4'] + $q->f['percent3'] + $q->f['percent2'] ) / 100;
            $g5 = $minamt <= $amount ? $q->f['back6'] / 100 * $amount * $q->f['percent255'] / 100 : 0;
            $d5 = $b5 * $q->f['percent255'] / 100;
            $r5 = 0 - $d5;
            $r6 = $amount * $q->f['percent255'] / 100;
            $this->oad += $r5;
            $this->omb += $b1;
            $this->oamount += $amount;
            $this->ogg += $g5;
            $strCmd = "Update {$x}bet_{$this->tbl} Set a1={$a1},b1={$b1},a2={$a2},b2={$b2},c2={$c2},d2={$d2},r2={$r2},a3={$a3},b3={$b3}" . ",c3={$c3},d3={$d3},r3={$r3},a4={$a4},b4={$b4},c4={$c4},d4={$d4},r4={$r4},a5={$a5},b5={$b5},c5={$c5},d5={$d5},e5={$e5},g5={$g5},r5={$r5},r6={$r6},rame=9 " . "where {$x}id=" . $q->f["{$x}id"] . " LIMIT 1";
			getdb($strCmd);
			$total=($b1>0?$b1+$amount:$a1);
			$uid=$q->f['user_id'];
			if(isset($userdata[$uid]))
			{
				$userdata[$uid]+=$total;
			}
			else
				$userdata[$uid]=$total;
			
            $q->next();
        }
        if ($x == "")
        {
            $strCmd = "DELETE FROM xbet_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
            $strCmd = "INSERT INTO xbet_{$this->tbl} (`id`, `user_id`, `lotto_id`, `createtime`, `bettype`, `subid`, `numbers`, `line_nums`, `amount`, `unitmoney`, `payoff`, `rate`, `rate1`, `rate2`, `rate3`, `rate4`, `rate5`, `a1`, `b1`, `a2`, `b2`, `c2`, `d2`, `e2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `disk`, `percent2`, `percent3`, `percent4`, `percent5`, `percent255`, `back1`, `back2`, `back3`, `back4`, `back5`, `back6`, `rame`, `state`, `ip`, `sessid`,`acl2`,`acl3`,`acl4`,`acl5` )" . " SELECT `id`, `user_id`, `lotto_id`, `createtime`, `bettype`, `subid`, `numbers`, `line_nums`, `amount`, `unitmoney`, `payoff`, `rate`, `rate1`, `rate2`, `rate3`, `rate4`, `rate5`, `a1`, `b1`, `a2`, `b2`, `c2`, `d2`, `e2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `disk`, `percent2`, `percent3`, `percent4`, `percent5`, `percent255`, `back1`, `back2`, `back3`, `back4`, `back5`, `back6`, `rame`, `state`, `ip`, `sessid`,`acl2`,`acl3`,`acl4`,`acl5` FROM bet_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
        }

		foreach($userdata as $key=>$money)
			UpBalance($this->gametype,$lotto_id,$key,$money);
		unset($userdata);
    }

    public function CalcBet2Open($lotto_id, $x = "")
    {
		$strTemp = "";
        $minamt = $this->GetConfig("minbackbet_{$this->tbl}");
        $q = getdb("!!Select * From {$x}bet2_{$this->tbl} Where lotto_id={$lotto_id} AND rame=0");
        while (!$q->eof)
        {
            $a2 = 0;
            $b2 = 0;
            $r2 = 0;
            $a3 = 0;
            $b3 = 0;
            $c3 = 0;
            $d3 = 0;
            $r3 = 0;
            $a4 = 0;
            $b4 = 0;
            $c4 = 0;
            $d4 = 0;
            $r4 = 0;
            $a5 = 0;
            $b5 = 0;
            $c5 = 0;
            $d5 = 0;
            $e5 = 0;
            $g5 = 0;
            $r5 = 0;
            $r6 = 0;
            $back2 = 100 - $q->f['back2'];
            $back3 = 100 - $q->f['back3'];
            $back4 = 100 - $q->f['back4'];
            $back5 = 100 - $q->f['back5'];
            $amount = $q->f['amount'];
            $acl = $q->f['acl'];
            $re = $this->CalcBetAmount($q);
            if ($q->f['rate'] < 1)
            {
                getdb("UPDATE {$x}bet2_{$this->tbl} set a2=0,b2=0,a3=0,b3=0,c3=0,d3=0,a4=0,b4=0,c4=0,d4=0" . ",a5=0,b5=0,c5=0,d5=0,e5=0,g5=0,r5=0,r6=0,rame=3 WHERE {$x}id = " . $q->f["{$x}id"] . " LIMIT 1");
                $errdoc = "({$this->tbl}){$strTemp}颁奖时异常走飞注单（I:" . $q->f['id'] . " B:" . $q->f['bettype'] . " Subid:" . $q->f['subid'] . " N:" . $q->f['numbers'] . " R:" . $q->f['rate'] . " A:" . $q->f['amount'] . "）";
                write_exceptional_log($q->f['user_id'], 1, $q->f['id'], $errdoc);
                $q->next();
                continue;
            }
            if ($re === 0)
            {
                getdb("UPDATE {$x}bet2_{$this->tbl} set a2=0,b2=0,a3=0,b3=0,c3=0,d3=0,a4=0,b4=0,c4=0,d4=0" . ",a5=0,b5=0,c5=0,d5=0,e5=0,g5=0,r5=0,r6=0,rame=9 WHERE {$x}id = " . $q->f["{$x}id"] . " LIMIT 1");
                $q->next();
                continue;
            }
            if ($acl <= 2)
            {
                $a2 = $minamt <= $amount ? $back2 / 100 * $amount : 0;
                $b2 = $re + $a2;
            } else
            {
                $back2 = 0;
            }
            if ($acl <= 3)
            {
                $a3 = $minamt < $amount ? ( $back3 - $back2 ) / 100 * $amount : 0;
                if ($acl == 3)
                {
                    $b3 = $re + $a3;
                    $c3 = $b3;
                    $d3 = $c3;
                } else
                {
                    $b3 = $b2 + $a3;
                    $c3 = $b3 * $q->f['percent3'] / 100;
                    $d3 = $b3 * ( 100 - $q->f['percent3'] ) / 100;
                    $r2 = $amount * $q->f['percent3'] / 100;
                }
            } else
            {
                $back3 = 0;
            }
            if ($acl <= 4)
            {
                $a4 = $minamt < $amount ? ( $back4 - $back3 ) / 100 * $amount : 0;
                if ($acl == 4)
                {
                    $b4 = $re + $a4;
                    $c4 = $b4;
                    $d4 = $c4;
                } else
                {
                    $b4 = $b3 + $a4;
                    $c4 = $b4 * $q->f['percent4'] / 100;
                    $d4 = $b4 * ( 100 - $q->f['percent3'] - $q->f['percent4'] ) / 100;
                    $r3 = $amount * $q->f['percent4'] / 100;
                }
                $a4 -= $a4 * $q->f['percent3'] / 100;
            } else
            {
                $back4 = 0;
            }
            if ($acl <= 5)
            {
                $a5 = $minamt < $amount ? ( $back5 - $back4 ) / 100 * $amount : 0;
                if ($acl == 5)
                {
                    $b5 = $re + $a5;
                    $c5 = $b5;
                    $d5 = $c5;
                } else
                {
                    $e5 = $re <= 0 ? 0 : $q->f['amount'] * $q->f['rate5'];
                    $b5 = $b4 + $a5 + $e5;
                    $c5 = $b5 * $q->f['percent5'] / 100;
                    $r4 = $amount * $q->f['percent5'] / 100;
                    $e5 -= $e5 * ( $q->f['percent4'] + $q->f['percent3'] ) / 100;
                }
                $a5 -= $a5 * ( $q->f['percent4'] + $q->f['percent3'] ) / 100;
            }
            $g5 = $minamt < $amount ? $q->f['back6'] / 100 * $amount * $q->f['percent255'] / 100 : 0;
            $d5 = $b5 * $q->f['percent255'] / 100;
            $r5 = 0 - $d5;
            $r6 = $amount * $q->f['percent255'] / 100;
            $this->oad += $r5;
            $this->ogg += $g5;
            $strCmd = "Update {$x}bet2_{$this->tbl} Set a2={$a2},b2={$b2},r2={$r2},a3={$a3},b3={$b3}" . ",c3={$c3},d3={$d3},r3={$r3},a4={$a4},b4={$b4},c4={$c4},d4={$d4},r4={$r4},a5={$a5},b5={$b5},c5={$c5},d5={$d5},e5={$e5},g5={$g5},r5={$r5},r6={$r6},rame=9 " . "where {$x}id=" . $q->f["{$x}id"] . " LIMIT 1";
            getdb($strCmd);
            $q->next();
        }
        if ($x == "")
        {
            $strCmd = "DELETE FROM xbet2_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
            $strCmd = "INSERT INTO xbet2_{$this->tbl} (`id`, `user_id`, `prev_id`, `uperid3`, `uperid4`, `uperid5`, `lotto_id`, `createtime`, `acl`, `bettype`,`subid`, `numbers`, `line_nums`, `amount`, `unitmoney`, `rate`, `rate2`, `rate3`, `rate4`, `rate5`, `a2`, `b2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `percent3`, `percent4`, `percent5`, `percent255`, `back2`, `back3`, `back4`, `back5`, `back6`, `betmode`, `rame`, `state`, `ip` )" . " SELECT `id`, `user_id`, `prev_id`, `uperid3`, `uperid4`, `uperid5`, `lotto_id`, `createtime`, `acl`, `bettype`,`subid`, `numbers`, `line_nums`, `amount`, `unitmoney`, `rate`, `rate2`, `rate3`, `rate4`, `rate5`, `a2`, `b2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `percent3`, `percent4`, `percent5`, `percent255`, `back2`, `back3`, `back4`, `back5`, `back6`, `betmode`, `rame`, `state`, `ip`  FROM bet2_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
        }
    }

    public function CalcBet8Open($lotto_id, $x = "")
    {
        $r5 = 0;
        $q = getdb("!!Select * From {$x}bet8_{$this->tbl} Where lotto_id={$lotto_id} AND rame=0");
        while (!$q->eof)
        {
            $amount = $q->f['amount'];
            $r5 = $this->CalcBetAmount($q) + $q->f['back'] / 100 * $amount;
            $this->oua += $amount;
            $this->our += $r5;
            getdb("Update {$x}bet8_{$this->tbl} Set r5={$r5}, rame=9 where {$x}id=" . $q->f["{$x}id"] . " LIMIT 1");
            $q->next();
        }
        if ($x == "")
        {
            $strCmd = "DELETE FROM xbet8_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
            $strCmd = "INSERT INTO xbet8_{$this->tbl} (`id`, `lotto_id`, `createtime`, `bettype`, `subid`, `numbers`, `line_nums`, `amount`, `rate`, `r5`, `back`, `rame`, `account`, `state`, `ip` )" . " SELECT `id`, `lotto_id`, `createtime`, `bettype`, `subid`, `numbers`, `line_nums`, `amount`, `rate`, `r5`, `back`, `rame`, `account`, `state`, `ip` FROM bet8_{$this->tbl} WHERE lotto_id={$lotto_id}";
            getdb($strCmd);
        }
    }

    public function CalcBetAmount($q, $chkFlag = false)
    {
        $bettype = $q->f['bettype'];
        $num = intval($q->f['numbers']);
        $rate = $q->f['rate'];
        $amount = $q->f['amount'];
        $subid = $q->f['subid'];
        if ($chkFlag)
        {
            if ($q->f['sessid'] == NULL)
            {
                return false;
            }
            if (chkBetSessid($q->f, $q->f['sessid']) != 1)
            {
                return false;
            }
        }
        $betSub = "{$bettype}";
        if (!isset($this->betname_3d[$betSub]))
        {
            return false;
        }
		$xamt=-$amount;
        switch ($q->f['bettype'])
        {
            case 0 : //三军 
				$arr=array($this->on1,$this->on2,$this->on3);
				$arr1=array_count_values($arr);
				foreach($arr1 as $key=>$val)
				{
					if($key==$num)
					{
						$xamt=((($rate-1)*$val+1)*$amount)-$amount;
						break;
					}
				}
                break;
            case 1 : //大小
                if ($this->bs==$num)
                    $xamt=$amount*($rate-1);
                break;
			 case 2 : //单双
                if ($this->sd==$num)
                    $xamt=$amount*($rate-1);
                break;
			case 3 : //短牌
				if ($this->dz==$num)
                    $xamt=$amount*($rate-1);
				break;
			case 4 : //豹子
				if ($this->bz==$num)
                    $xamt=$amount*($rate-1);
				break;
			case 5 : //全骰
				if ($this->bz>0)
                    $xamt=$amount*($rate-1);
				break;
			case 6 : //长牌
				$arr=array($this->on1,$this->on2,$this->on3);
				$tmp=array(array(-1,-1),array(1,2),array(1,3),array(1,4),array(1,5),array(1,6),
					array(2,3),array(2,4),array(2,5),array(2,6),array(3,4),array(3,5),array(3,6),
					array(4,5),array(4,6),array(5,6));
				if(in_array($tmp[$num][0],$arr) && in_array($tmp[$num][1],$arr))
                    $xamt=$amount*($rate-1);
				break;
			case 7 : //总点数
				$tmp=array(-1,4,5,6,7,8,9,10,11,12,13,14,15,16,17);
				if ($this->zd==$tmp[$num])
                    $xamt=$amount*($rate-1);
				break;
			case 8 : 
				if ($this->dz>0 && $num==1) //对
                    $xamt=$amount*($rate-1);
				elseif ($this->dz==0 && $num==2) //散号
					$xamt=$amount*($rate-1);	
				break;
        }
        return $xamt;
    }

    public function getBet8_3D($id)
    {
        $cmdStr = "";
        if (0 < $id)
        {
            $q = getdb("!!Select * From bet8_{$this->tbl} where id={$id} AND IFNULL(state,0)=0 and (rame=0 or rame=2)");
        } else
        {
            $q = getdb("!!Select * From bet8_{$this->tbl} where IFNULL(state,0)=0 and (rame=0 or rame=2)");
        }
        $this->m_lotto_id = 0;
        while (!$q->eof)
        {
            $this->m_lotto_id = $q->f['lotto_id'];
            if ($this->bettotal_by_lotto_id)
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}_{$this->m_lotto_id}";
            } else if ($paperOpen = $this->getPaperOpen($q->f['lotto_id']))
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}_{$paperOpen}";
            } else
            {
                $this->bettotal_3d = "bettotal_{$this->tbl}";
            }
            $this->SetBet8Total($q);
            getdb("Update bet8_{$this->tbl} Set state=1 Where id=" . $q->f['id'] . " LIMIT 1");
            $q->next();
        }
    }

    public function SetBet8Total($sqlQ)
    {
        $lotto_id = $sqlQ->f['lotto_id'];
        $bettype = $sqlQ->f['bettype'];
        $subid = $sqlQ->f['subid'];
        $numbers = $sqlQ->f['numbers'];
        $rate = $sqlQ->f['rate'];
        $back = $sqlQ->f['back'];
        $rame = $sqlQ->f['rame'] == 2 ? 0 - 1 : 1;
        $amount = $sqlQ->f['amount'] * $rame;
        $isgroup36 = false;
        $isR1Pass = false;
        $u_id = "0";
        $back = $amount * $back / 100;
        $nums = array();
        if ($bettype == 46)
        {
            $nums = explode(",", $numbers);
            $isR1Pass = true;
        } else if (3 < strlen($numbers))
        {
            $i = 0;
            while ($i < strlen($numbers))
            {
                array_push($nums, substr($numbers, $i, 1));
                ++$i;
            }
            $isgroup36 = true;
        } else
        {
            array_push($nums, $numbers);
        }
        if ($isR1Pass)
        {
            $rate = round(pow($rate, 0.33333), 3);
        }
        $cmdStr = "";
        $amount2 = 0;
        $amount3 = round($amount * $rate, 0);
        $amount4 = 0;
        if (!$isgroup36 && !$isR1Pass)
        {
            $sqlBetTotal = getdb("!!Select * From {$this->bettotal_3d} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$bettype} and subid={$subid} and numbers='{$nums[0]}'");
            $amount4 = round($amount * $rate, 0);
            if ($sqlBetTotal->eof)
            {
                $cmdStr = "Insert Into {$this->bettotal_3d} " . "(lotto_id, user_id ,bettype,subid,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d," . "updatetime" . ") Values (" . $lotto_id . ",0," . $bettype . "," . $subid . "," . "'" . $nums[0] . "',1,0," . ( $amount * ( 0 - 1 ) ) . ",0," . ( $amount4 * ( 0 - 1 ) ) . ",0,0,{$back},0,0";
                $k = 1;
                while ($k <= 4)
                {
                    $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                    ++$k;
                }
                $cmdStr .= "," . systime();
                $cmdStr .= ")";
            } else
            {
                $cmdStr = "Update {$this->bettotal_3d} Set " . "totalnum = totalnum +" . $rame . ",amount2 = amount2 -" . $amount . ",amount4 = amount4 -" . $amount4 . ",back2 = back2 +" . $back . ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=0" . " and bettype=" . $bettype . " and subid=" . $subid . " and numbers='" . $nums[0] . "' LIMIT 1";
            }
            getdb($cmdStr);
        } else
        {
            $j = 0;
            while ($j < count($nums))
            {
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_3d} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$bettype} and subid = {$subid} and numbers='{$nums[$j]}'");
                if ($sqlBetTotal->eof)
                {
                    $amount4 = round($amount * $rate, 0);
                    $cmdStr = "Insert Into {$this->bettotal_3d} " . "(lotto_id, user_id ,bettype,subid,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d," . "updatetime" . ") Values (" . $lotto_id . ",0," . $bettype . "," . $subid . "," . "'" . $nums[$j] . "',1,0," . ( $amount * ( 0 - 1 ) ) . ",0," . ( $amount4 * ( 0 - 1 ) ) . ",0,0,{$back},0,0";
                    $k = 1;
                    while ($k <= 4)
                    {
                        $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                } else
                {
                    $amount4 = round($amount * $rate, 0);
                    $cmdStr = "Update {$this->bettotal_3d} Set " . "totalnum = totalnum +" . $rame . ",amount2 = amount2 -" . $amount . ",amount4 = amount4 -" . $amount4 . ",back2 = back2 +" . $back . ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=0" . " and bettype=" . $bettype . " and subid=" . $subid . " and numbers='" . $nums[$j] . "' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
                ++$j;
            }
        }
    }

    public function ReBetTotalByLottoId($lotto_id)
    {
        if ($this->bettotal_by_lotto_id)
        {
            $this->bettotal_3d = "bettotal_{$this->tbl}_{$lotto_id}";
        } else if ($paperOpen = $this->getPaperOpen($lotto_id))
        {
            $this->bettotal_3d = "bettotal_{$this->tbl}_{$paperOpen}";
        } else
        {
            $this->bettotal_3d = "bettotal_{$this->tbl}";
        }
        getdb("Update bet_{$this->tbl} Set state=0,rame=0 where lotto_id={$lotto_id} and (rame=0 or rame=9)");
        getdb("Update bet2_{$this->tbl} Set state=0,rame=0 where lotto_id={$lotto_id} and (rame=0 or rame=9)");
        getdb("Update bet8_{$this->tbl} Set state=0,rame=0 where lotto_id={$lotto_id} and (rame=0 or rame=9)");
        getdb("delete from {$this->bettotal_3d} where lotto_id = {$lotto_id}");
        getdb("delete from autorate_{$this->tbl} where lotto_id = {$lotto_id}");
        $this->autoport = false;
        if ($this->GetConfig("protect_start_{$this->tbl}") !== "0" && $this->GetConfig("autoportmod_{$this->tbl}") !== "0")
        {
            $this->autoport = 1;
        }
        $this->betmin = floatval($this->GetConfig("betmin_{$this->tbl}"));
        $this->autoport = false;
        if ($this->GetConfig("protect_start_{$this->tbl}") !== "0" && $this->GetConfig("autoportmod_{$this->tbl}") !== "0")
        {
            $this->autoport = true;
        }
        $rs = getdb("Select * From bet2_{$this->tbl} Where IFNULL(state,0)=0 and (rame=0 or rame=2) order by acl DESC ,id");
        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            $this->m_lotto_id = $rs->f['lotto_id'];
            $this->SetBet2Total($rs);
            $cmdStr = "Update bet2_{$this->tbl} Set state=1 Where id=" . $rs->f['id'] . " LIMIT 1";
            getdb($cmdStr);
            $rs->next();
        }
        $rs = getdb("Select * From bet_{$this->tbl} Where IFNULL(state,0)=0 and (rame=0 or rame=2) order by id");
        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            $this->m_lotto_id = $rs->f['lotto_id'];
            $this->SetBetTotal($rs);
            $cmdStr = "Update bet_{$this->tbl} Set state=1 Where id=" . $rs->f['id'] . " LIMIT 1";
            getdb($cmdStr);
            $rs->next();
        }
        $rs = getdb("Select * From bet8_{$this->tbl} where IFNULL(state,0)=0 and (rame=0 or rame=2) order by id");
        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            $this->m_lotto_id = $rs->f['lotto_id'];
            $this->SetBet8Total($rs);
            getdb("Update bet8_{$this->tbl} Set state=1 Where id=" . $rs->f['id'] . " LIMIT 1");
            $rs->next();
        }
        $rs = getdb("SELECT MAX(createtime) FROM bet_{$this->tbl} WHERE lotto_id=" . $lotto_id);
        getdb("update bet2_{$this->tbl} set createtime = " . $rs->f[0] . " where lotto_id=" . $lotto_id . " and betmode=1");
    }

    public function LOG($str)
    {
        $add_str = $str . "\n";
        if (!( $fp = fopen("lotto_3d_log.txt", "a") ))
        {
            echo "open error";
        } else
        {
            fwrite($fp, $add_str);
            fpassthru($fp);
        }
    }

    public function getPaperOpen($lotto_id)
    {
        $sql = "SELECT open FROM lotto_{$this->tbl} WHERE id={$lotto_id} LIMIT 0,1";
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $date = $rs->f[0];
            $dateArr = explode("-", $date);
            return $dateArr[0] . $dateArr[1] . $dateArr[2];
        }
        return false;
    }

}

?>